﻿using System;
using System.Web;
using umbraco.presentation.masterpages;
using TheOutfield.UmbExt.PageNotifications.Util;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.web;
using umbraco;

namespace TheOutfield.UmbExt.PageNotifications
{
    public class AppBase : umbraco.BusinessLogic.ApplicationBase
    {
        public AppBase()
        {
            umbracoPage.Load += new MasterPageLoadHandler(umbracoPage_Load);
        }

        protected void umbracoPage_Load(object sender, EventArgs e)
        {
            var rootControl = sender as umbracoPage;
            if (rootControl == null)
                return;

            var path = rootControl.Page.Request.Path.ToLower();
            if (!path.Contains("editcontent.aspx"))
                return;

            var placeHolder = (ContentPlaceHolder)ControlUtil.FindControlRecursive(rootControl, "body");
            if (placeHolder == null)
                return;

            var currentNode = new Document(Convert.ToInt32(HttpContext.Current.Request["id"]));
            if (currentNode == null)
                return;

            var lang = System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;

            foreach (var notification in Config.Instance.Notifications)
            {
                if (notification.Messages.ContainsKey(lang))
                {
                    switch (notification.Type)
                    {
                        case NotificationType.Redirect:
                            HandleRedirectNotification(placeHolder, currentNode, notification.Messages[lang]);
                            break;
                        case NotificationType.InternalRedirect:
                            HandleInternalRedirectNotification(placeHolder, currentNode, notification.Messages[lang]);
                            break;
                        case NotificationType.NaviHide:
                            HandleNaviHideNotification(placeHolder, currentNode, notification.Messages[lang]);
                            break;
                        case NotificationType.UnPublished:
                            HandleUnPublishedNotification(placeHolder, currentNode, notification.Messages[lang]);
                            break;
                        case NotificationType.NoTemplate:
                            HandleNoTemplateNotification(placeHolder, currentNode, notification.Messages[lang]);
                            break;

                    }
                }
            }
        }

        protected void HandleRedirectNotification(ContentPlaceHolder placeHolder, Document doc, string msg)
        {
            var redirectId = 0;

            var umbRedirectProp = doc.getProperty("umbracoRedirect");
            if (umbRedirectProp != null && umbRedirectProp.Value != null && umbRedirectProp.Value.ToString() != "")
                redirectId = Convert.ToInt32("0" + umbRedirectProp.Value.ToString());

            if (redirectId > 0)
            {
                var redirectNode = new Document(redirectId);

                ShowNotification(placeHolder, string.Format(msg,
                    redirectNode.Text,
                    VirtualPathUtility.ToAbsolute(GlobalSettings.Path) + "/editContent.aspx?id="+ redirectId));
            }
        }
         
        protected void HandleInternalRedirectNotification(ContentPlaceHolder placeHolder, Document doc, string msg)
        {
            var redirectId = 0;

            var umbRedirectProp = doc.getProperty("umbracoInternalRedirectId");
            if (umbRedirectProp != null && umbRedirectProp.Value != null && umbRedirectProp.Value.ToString() != "")
                redirectId = Convert.ToInt32("0" + umbRedirectProp.Value.ToString());

            if (redirectId > 0)
            {
                var redirectNode = new Document(redirectId);

                ShowNotification(placeHolder, string.Format(msg,
                    redirectNode.Text,
                    VirtualPathUtility.ToAbsolute(GlobalSettings.Path) + "/editContent.aspx?id=" + redirectId));
            }
        }

        protected void HandleNaviHideNotification(ContentPlaceHolder placeHolder, Document doc, string msg)
        {
            var naviHideProp = doc.getProperty("umbracoNaviHide");
            if (naviHideProp != null && naviHideProp.Value != null && naviHideProp.Value.ToString().Trim() == "1")
            {
                ShowNotification(placeHolder, msg);
            }
        }

        protected void HandleUnPublishedNotification(ContentPlaceHolder placeHolder, Document doc, string msg)
        {
            if (!doc.Published)
            {
                ShowNotification(placeHolder, msg);
            }
        }

        protected void HandleNoTemplateNotification(ContentPlaceHolder placeHolder, Document doc, string msg)
        {
            if (doc.Template <= 0)
            {
                ShowNotification(placeHolder, msg);
            }
        }

        protected void ShowNotification(ContentPlaceHolder placeHolder, string msg)
        {
            var panel = new Panel();
            panel.Style.Add("background-color", "#fff9c2");
            panel.Style.Add("border", "solid 1px #ffd800");
            panel.Style.Add("color", "#333333");
            panel.Style.Add("padding", "10px");
            panel.Style.Add("margin-bottom", "10px");

            var literal = new Literal();
            literal.Text = msg;

            panel.Controls.Add(literal);

            placeHolder.Controls.AddAt(0, panel);
        }
    }
}